package com.example.a2306_shop.fg

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.a2306_shop.R
import com.example.a2306_shop.adapter.OneAdapter
import com.example.a2306_shop.adapter.ProductAdapter
import com.example.a2306_shop.adapter.TwoAdapter
import com.example.a2306_shop.databinding.FragmentCategoryBinding
import com.example.lib_base.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

// TODO: Rename parameter arguments, choose names that match

class CategoryFragment : BaseFragment<FragmentCategoryBinding,CategoryViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var productAdapter: ProductAdapter

    var cid = 0//当前分类
    var keyword = ""//关键词 没发挥作用
    var page = 1//当前页面
    var limit = 3//每页数据条数  超过5 不容易出现加载更多效果
    override fun initData() {
        viewModel.categorySuccess.observe(this){
            if(it.code==200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()

                //二级分类数据加载   加载的是 一级分类数据中的第一个的child
                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data[0].child)
                twoAdapter.notifyDataSetChanged()

                //三级分类的数据实现请求
                cid = twoAdapter.data[0].id
                viewModel.productCategory(cid, keyword, limit, page)
            }
        }
        viewModel.productSuccess.observe(this){
            if(it.code==200){
                //当page=1时，数据必须清空  其他情况不能清空
                if(page==1){
                    productAdapter.data.clear()
                }
                productAdapter.data.addAll(it.data.list)
                productAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
       oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,3)

        viewModel.category()
        //初始化三级列表 即商品列表
        productAdapter = ProductAdapter()
        binding.rvProduct.adapter = productAdapter
        binding.rvProduct.layoutManager = LinearLayoutManager(context)

        //点击一级分类 二级分类数据刷新
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            //一级分类选中的发生变化
            oneAdapter.setType(oneAdapter.data[position].id)
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
            //三级列表重新调用
            page = 1
            cid = twoAdapter.data[0].id
            twoAdapter.setType(cid)
            viewModel.productCategory(cid, keyword, limit, page)
        }
        //点击二级分类，三级数据进行刷新
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            cid = twoAdapter.data[position].id
            twoAdapter.setType(cid)
            page = 1
            viewModel.productCategory(cid, keyword, limit, page)
        }
        binding.smart.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.productCategory(cid, keyword, limit, page)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.productCategory(cid, keyword, limit, page)
                binding.smart.finishLoadMore()
            }
        })
    }

    override fun getViewModel(): Class<CategoryViewModel>  = CategoryViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_category

}